package com.rongc.client.freight.business.mine.view.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.rongc.client.core.gson.GsonHelper;
import com.rongc.client.core.network.code.HandleCode;
import com.rongc.client.core.network.http.RequestManager;
import com.rongc.client.core.utils.ActivityUtils;
import com.rongc.client.core.utils.LogUtils;
import com.rongc.client.core.utils.StringUtils;
import com.rongc.client.freight.R;
import com.rongc.client.freight.UniApplication;
import com.rongc.client.freight.base.BaseActivity;
import com.rongc.client.freight.base.view.widget.DividerItemDecoration;
import com.rongc.client.freight.business.mine.model.WalletBean;
import com.rongc.client.freight.business.mine.request.api.MineWalletApi;
import com.rongc.client.freight.business.mine.view.adapter.RecyclerWalletAdapter;
import com.rongc.client.freight.utils.UtilMethod;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

import static com.flyco.dialog.widget.NormalDialog.STYLE_TWO;

/**
 * Created by ccqx on 2017/7/17.
 */

public class MineWalletActivity extends BaseActivity implements View.OnClickListener{
    @Bind(R.id.tv_balance)
    TextView mTvBalance;
    @Bind(R.id.recycler)
    RecyclerView mRecyclerView;
    RecyclerWalletAdapter modelAdapter;

    List<WalletBean> datas;
    NormalDialog dialog ;

    Response.Listener<JSONObject> respOrderListener = new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            if (HandleCode.requestSuccess()) {
                String price = StringUtils.parseDouble(response.optString("price"))+"";
                mTvBalance.setText(price);
                UniApplication.getInstance().getUserInfo().setBalance(price);
                UniApplication.getInstance().getUserInfo().save();
                WalletBean[] beans = GsonHelper.getDeserializer().fromJson(response.optString("list"), WalletBean[].class);
                datas.clear();
                if(beans != null)
                    for (int i=0;i<beans.length;i++){
                        datas.add(beans[i]);
                    }
                modelAdapter.notifyDataSetChanged();
            } else {
                UtilMethod.dismissProgressDialog(MineWalletActivity.this);
            }
        }
    };

    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            LogUtils.i("error.getMessage(): " + error.getMessage());
            UtilMethod.dismissProgressDialog(MineWalletActivity.this);
        }
    };


    @OnClick({R.id.lyt_tixian,R.id.lyt_yhq})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.lyt_tixian:
                if(StringUtils.isEmpty(UniApplication.getInstance().getUserInfo().getPayPwd())){
                    dialog.show();
                }else{
                    ActivityUtils.startActivity(MineWalletActivity.this,TixianActivity.class);
                }

                break;
            case R.id.lyt_yhq:
                if(StringUtils.isEmpty(UniApplication.getInstance().getUserInfo().getPayPwd())){
                    dialog.show();
                }else {
                    ActivityUtils.startActivity(MineWalletActivity.this, ChongzhiActivity.class);
                }
                break;
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        RequestManager.getInstance().call(new MineWalletApi(
                new MineWalletApi.MineWalletParams(),
                respOrderListener, errorListener));
    }


    @Override
    public void initView() {
        initToolbar(R.string.wallet_title);

        datas = new ArrayList<>();
        modelAdapter = new RecyclerWalletAdapter(this,datas);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        mRecyclerView.setAdapter(modelAdapter);
        mTvBalance.setText(UniApplication.getInstance().getUserInfo().getBalance());

        dialog = new NormalDialog(this);
        dialog.content("请设置支付密码")
                .style(STYLE_TWO)
                .btnText(new String[]{"取消","设置"})
                .setOnBtnClickL(new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        dialog.cancel();
                    }
                },new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        if(StringUtils.isEmpty(UniApplication.getInstance().getUserInfo().getPayPwd()))
                            ActivityUtils.startActivity(MineWalletActivity.this,PasswordPayActivity.class);
                        else
                            ActivityUtils.startActivity(MineWalletActivity.this,TixianActivity.class);

                        dialog.cancel();

                    }
                });
    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_wallet_host;
    }
}
